package com.example.zhaobo.videoplayertest;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.VideoView;

import java.io.IOException;

/**
 * 视频播放的三种方式
 * http://blog.csdn.net/itachi85/article/details/7216962
 */
public class MainActivity extends AppCompatActivity {
    private String videoPath = "http://video.ys.myrunners.com:90/2017/0329/c548016cf6977de32dcd40d174831db9.mp4";
    //    private String videoPath = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
    private MenuListView menu;
    private VideoView video_view;
    private SurfaceView sv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        sv = (SurfaceView) findViewById(R.id.sv);
        menu = (MenuListView) findViewById(R.id.menu);
        video_view = (VideoView) findViewById(R.id.video_view);
        menu.add("内置播放器", new MenuListView.CallBack() {
            @Override
            public void doFunc() {
                Uri uri = Uri.parse(videoPath);
                //调用系统自带的播放器
                Intent intent = new Intent(Intent.ACTION_VIEW);
                Log.v("URI:::::::::", uri.toString());
                intent.setDataAndType(uri, "video/mp4");
                startActivity(intent);
            }
        });

        menu.add("VideoView", new MenuListView.CallBack() {
            @Override
            public void doFunc() {
                Uri uri = Uri.parse(videoPath);
                video_view.setMediaController(new MediaController(MainActivity.this));
                video_view.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {

                    }
                });
                video_view.setVideoURI(uri);
                video_view.start();
            }
        });

        menu.add("MediaPlayer", new MenuListView.CallBack() {
            @Override
            public void doFunc() {
                player.start();
            }
        });
        menu.notifyDataSetChanged();
        initMedia();
    }

    private SurfaceHolder holder;
    private MediaPlayer player;
    private Display currDisplay;
    private int vWidth, vHeight;

    private void initMedia() {
        holder = sv.getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                // 当SurfaceView中的Surface被创建的时候被调用
                //在这里我们指定MediaPlayer在当前的Surface中进行播放
                player.setDisplay(holder);
                //在指定了MediaPlayer播放的容器后，我们就可以使用prepare或者prepareAsync来准备播放了
                player.prepareAsync();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });
        //下面开始实例化MediaPlayer对象
        player = new MediaPlayer();
//        player.setOnCompletionListener(this);
//        player.setOnErrorListener(this);
//        player.setOnInfoListener(this);
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                // 当prepare完成后，该方法触发，在这里我们播放视频

                //首先取得video的宽和高
                vWidth = player.getVideoWidth();
                vHeight = player.getVideoHeight();

                if (vWidth > currDisplay.getWidth() || vHeight > currDisplay.getHeight()) {
                    //如果video的宽或者高超出了当前屏幕的大小，则要进行缩放
                    float wRatio = (float) vWidth / (float) currDisplay.getWidth();
                    float hRatio = (float) vHeight / (float) currDisplay.getHeight();

                    //选择大的一个进行缩放
                    float ratio = Math.max(wRatio, hRatio);

                    vWidth = (int) Math.ceil((float) vWidth / ratio);
                    vHeight = (int) Math.ceil((float) vHeight / ratio);

                    //设置surfaceView的布局参数
                    sv.setLayoutParams(new LinearLayout.LayoutParams(vWidth, vHeight));

                    //然后开始播放视频

                    player.start();
                } else {
                    player.start();
                }
            }
        });
//        player.setOnSeekCompleteListener(this);
//        player.setOnVideoSizeChangedListener(this);
        try {
            player.setDataSource(videoPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //然后，我们取得当前Display对象
        currDisplay = this.getWindowManager().getDefaultDisplay();
    }
}
